<?php

/**
 * @name DashboardController.php
 * @copyright (c) 2011
 * @author Arsene Tochemey GANDOTE<arsene.gandote@yahoo.com>
 */
class DashboardController extends AppController {
    # Controller name

    var $name = 'Dashboard';
    # set the model to use
    var $uses = array('User','Ticket');
    # Set the layout to use
    var $layout = 'default';

    # Controllers Actions
    /**
     * This function checks the authenticity of the user.
     * @name beforeFilter
     * @param void
     * @return void
     * @access public
     * */

    function beforeFilter($moduleName = null) {
        parent::beforeFilter();


    }

    /**
     * This is a kind of home page of the controller Dashboard.
     * @name index
     * @param void
     * @var mixed $loggedUser array containing the logged user session data.
     * @return void
     * @access public
     * */
    function index() {
        $data_table = $this->getAssignedTickets();
        $user_ticket_stats = $this->getUsetTicketStats();

        $group_modules = $this->getGroupModules();
        $can_create_ticket = $this->validateModules($group_modules,'Tickets');

        $controller = $this;
        $title_for_layout = 'Dashboard';
        $page_description = 'Your Dashboard';
        $this->set(compact('controller','title_for_layout','page_description','data_table','user_ticket_stats','can_create_ticket'));
    }

    function getAssignedTickets(){
        $condition_array = array(
            'Ticket.user_id' => $this->Auth->user('id'),
            'Ticket.deleted' => 'n'
        );
        $data_table = $this->Ticket->find('all', array(
            'fields'=>array('Ticket.id','Ticket.subject','Ticket.status_id','Ticket.priority_id','Ticket.created','Ticket.modified'),
            'conditions' => $condition_array,
            'contain'=> array(
                'Status'=>array('fields' => array('Status.id', 'Status.name','Status.set_end')),
                'Priority'=>array('fields' => array('Priority.id', 'Priority.name'))
            ),
            'order' => array('Ticket.id'=>'desc'),
            'recursive' => 1
        ));

        return $data_table;
    }


    function getUsetTicketStats(){
        $return = array(
            'assigned_ticket_total'=>'',
            'created_ticket_total'=>'',
            'closed_ticket_total'=>'',
        );

        //Get Assigned Ticket Totals
        $condition_array = array(
            'Ticket.user_id' => $this->Auth->user('id'),
            'Ticket.deleted' => 'n'
        );
        $data_table = $this->Ticket->find('count', array(
            'conditions' => $condition_array,
            'recursive' => -1
        ));
        $return['assigned_ticket_total']=$data_table;

        //Get Created Ticket Totals
        $condition_array = array(
            'Ticket.created_by' => $this->Auth->user('id'),
            'Ticket.deleted' => 'n'
        );
        $data_table = $this->Ticket->find('count', array(
            'conditions' => $condition_array,
            'recursive' => -1
        ));
        $return['created_ticket_total']=$data_table;

        //Get Closed Ticket Totals
        $condition_array = array(
            'Status.set_end' => 'Closed',
            'Ticket.modified_by' => $this->Auth->user('id'),
            'Ticket.deleted' => 'n'
        );
        $data_table = $this->Ticket->find('count', array(
            'conditions' => $condition_array,
            'contain'=>array('Status'),
            'recursive' => 1
        ));
        $return['closed_ticket_total']=$data_table;

        return $return;
    }



}